package com.dnjn.happiness.survey.surveyHandler;

import com.dnjn.happiness.common.amqp.AmqpSender;
import com.dnjn.happiness.common.core.constant.HappyConstants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.SysJobVO;
import com.dnjn.happiness.common.core.utils.DateUtils;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.core.score.vo.HappySurvey;
import com.dnjn.happiness.common.core.score.vo.HappyVO;
import com.dnjn.happiness.job.api.RemoteJobService;
import com.dnjn.happiness.survey.domain.Survey;
import com.dnjn.happiness.survey.domain.SurveyRecord;
import com.dnjn.happiness.survey.service.ISurveyRecordService;
import com.dnjn.happiness.survey.service.ISurveyService;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component("surveyHandler")
public class SurveyAndTraingHandler {
    private static final String READY = "READY";
    private static final String PROCESSING = "PROCESSING";
    private static final String FINISHED = "FINISHED";
    @Autowired
    private ISurveyService surveyService;
    @Autowired
    private RemoteJobService jobService;


    @Autowired
    private ISurveyRecordService recordService;

    

    /**
     * 问卷
     *
     * @param surveyId 问卷id
     * @param jobId    定时任务id
     */
    public void theSurveyToStart(String surveyId, String jobId) throws Exception {
        //          开始问卷
        surveyService.lambdaUpdate().set(Survey::getSurveyStatus, "PROCESSING").eq(Survey::getSurveyId, surveyId).update();
        //仅仅运行一次
        SysJobVO sysJobVO = new SysJobVO();
        R<SysJobVO> result = jobService.getInfoById(Long.parseLong(jobId), SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            sysJobVO = result.getData();
        }
        sysJobVO.setStatus("1");
        jobService.changeStatusApi(sysJobVO, SecurityConstants.INNER);
    }


    public void theSurveyToEnd(String surveyId, String jobId) throws Exception {
        //     surveyService.endSurveyId(Long.parseLong(surveyId));
        //结束问卷
        surveyService.lambdaUpdate().set(Survey::getSurveyStatus, "FINISHED").eq(Survey::getSurveyId, surveyId).update();
        // todo 结束问卷发mq
        List<SurveyRecord> list = recordService.queryRecordBySurveyIdIsNot(Long.parseLong(surveyId));

        list.stream().forEach(item->{
            HappyVO happyVO = new HappyVO();
            happyVO.setRtype(HappyConstants.EMO_USER_RTYPE_SURVEY);
            happyVO.setUserId(item.getUserId());
            HappySurvey happySurvey = new HappySurvey();
            happySurvey.setNotJoin(1);
            happyVO.setSurvey(happySurvey);
            AmqpSender.sendArgs(happyVO);

        });
        //仅仅运行一次
        SysJobVO newJob = new SysJobVO();
        R<SysJobVO> result = jobService.getInfoById(Long.parseLong(jobId), SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            newJob = result.getData();
        }
        newJob.setStatus("1");
        jobService.changeStatusApi(newJob, SecurityConstants.INNER);

    }


    /**
     * 定时五分钟扫描一次问卷表,如果存在 需要开始的问卷更新开始状态 -PROCESSING,如果存在需要结束的问卷更新结束状态 -FINISHED
     */
//    @Scheduled(cron = "0 0/5 * * * ?")
    public void updateSurveyStatus() {
        log.info("监听问卷表start=========");
        surveyService.lambdaUpdate().eq(Survey::getDeleted, "0").eq(Survey::getSurveyStatus, READY).le(Survey::getDateStart, DateUtils.getNowDate())
                .set(Survey::getSurveyStatus, PROCESSING)
                .update();
        surveyService.lambdaUpdate().eq(Survey::getDeleted, "0").eq(Survey::getSurveyStatus, PROCESSING).le(Survey::getDateEnd, DateUtils.getNowDate())
                .set(Survey::getSurveyStatus, FINISHED)
                .update();
        log.info("监听问卷表end=========");
    }

}
